package com.vstudy.zx.service.impl;

import com.vstudy.zx.dao.ArticleDao;
import com.vstudy.zx.dao.ArticleManageDao;
import com.vstudy.zx.dao.GradeDao;
import com.vstudy.zx.dao.SubjectDao;
import com.vstudy.zx.entity.Article;
import com.vstudy.zx.entity.Grade;
import com.vstudy.zx.entity.Subject;
import com.vstudy.zx.service.ArticleManageService;
import com.vstudy.zx.utils.IOUtil;
import com.vstudy.zx.utils.page.ArticlePage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ResourceUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public class ArticleManageServiceImpl implements ArticleManageService {
    @Autowired
    private ArticleManageDao articleManageDao;
    @Autowired
    private GradeDao gradeDao;
    @Autowired
    private SubjectDao subjectDao;
    @Autowired
    private ArticleDao articleDao;

    @Override
    public List<Article> getArticlePage(ArticlePage page, Long te_id, Integer grade_id, Long subject_id) {
        List<Article> articles = articleManageDao.selectPage(page, te_id, grade_id, subject_id); // 数据库自动返回page.totalCount
        if (page.getPageIndex() > page.getPageCount()) {
            page.setPageIndex(page.getPageCount());
        }
        if (page.getPageIndex() < 1) {
            page.setPageIndex(1L);
        }
        Long count = articleManageDao.count(page);
//        page.setCount(count);
        return articles;
    }

    @Override
    public List<Grade> getGrades() {

        return gradeDao.select();
    }

    @Override
    @Transactional(readOnly = true)
    public List<Subject> getSubjects() {
        return subjectDao.select();
    }

    @Override
    @Transactional(readOnly = true)
    public Article getArticleById(Long id) {
        return articleDao.selectById(id);
    }

    @Override
    public boolean updateArticle(String articleHtml, String title, Long articleId, HttpServletRequest request) {
        Article article = articleDao.selectById(articleId);
        article.setArticle_title(title);  // 标题
        try {
            String basePath = ResourceUtils.getURL("classpath:").getPath();
//            String realPath = request.getServletContext().getRealPath(article.getArticle_content());
            String realPath = basePath + article.getArticle_content();
            IOUtil.writeArticle(realPath, "utf-8", articleHtml);  // 内容
            int insert = articleDao.update(article);
            if (insert < 1) {
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return true;
    }

    @Override
    public int removeArticle(Long id, HttpServletRequest request) {
        Article article = articleDao.selectById(id);
        String realPath = request.getServletContext().getRealPath(article.getArticle_content());
        // 删除记录和文件
        if (IOUtil.deleteFile(realPath)) {
            int rs = articleDao.delete(id);

            return rs;
        }
        return 0;
    }
}
